﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using DomesticPestLib;
using System.Collections;

namespace DomesticPestProject
{
    public partial class frmInformation : System.Web.UI.Page
    {
        DLayer dlayer;
        ArrayList objalst;
        entyInfo entyinfo;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnInformation.Enabled = false;
                ddlJourn.Visible = true;
                txtJourn.Visible =false;
                BindDetails();
                clear();
                //txtNote.text = Session["UserID"].ToString();
            }
               
        }

        private void BindDetails()
        {
                entyinfo = new entyInfo();
                dlayer = new DLayer();
                objalst = new ArrayList();
                objalst = dlayer.selectInfo();
                gvInfo.DataSource = objalst;
                gvInfo.DataBind();
         }

        private void clear()
        {
            txtAthr.Text = "";
            txtcd.Text = "";
            txtJourn.Text = "";
            txtNote.Text = "";
            txtPgRef.Text = "";
            txtTPub.Text = "";
            txtVol.Text = "";
            txtYr.Text = "";
        }
        protected void btnInformation_Click(object sender, EventArgs e)
        {
            //Response.Redirect("");
        }
        
        protected void btnMain_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmMain.aspx");
        }

        protected void btnCommodity_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmCommodity.aspx");
        }

        protected void btnTransaction_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmTransaction.aspx");
        }

        protected void btnPest_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPest.aspx");
        }

        protected void btnLctn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmInformation.aspx");
        }

        protected void btnPsDsrbtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPestDistribution.aspx");
        }

        protected void txtNote_TextChanged(object sender, EventArgs e)
        {

        }

        protected void gvInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string infocode = gvInfo.Rows[e.RowIndex].Cells[2].Text;
            (new DLayer()).DeleteInfo(infocode);
            BindDetails();
        }

        protected void gvInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvInfo.EditIndex = e.NewEditIndex;
            BindDetails();
        }

        protected void gvInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            entyInfo info = new entyInfo();
                      
            TextBox txtcd = gvInfo.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
            TextBox txtAthr = gvInfo.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
            TextBox txtYr = gvInfo.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
            TextBox txtTPub = gvInfo.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
            TextBox txtJourn = gvInfo.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;
            TextBox txtVol = gvInfo.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox;
            TextBox txtPgRef = gvInfo.Rows[e.RowIndex].Cells[8].Controls[0] as TextBox;
            TextBox txtNote = gvInfo.Rows[e.RowIndex].Cells[9].Controls[0] as TextBox;

            info.Author = txtAthr.Text + " ";
            info.InfoCd = txtcd.Text;
            info.Year = txtYr.Text;
            info.Title = txtTPub.Text;
            info.Journal = txtJourn.Text;
            info.Vol = txtVol.Text;
            info.Pageref = txtPgRef.Text;
            info.Note = txtNote.Text;

            DLayer d = new DLayer();
            (new DLayer()).UpdateInfo(info);
            gvInfo.EditIndex = -1;
            BindDetails();
        }

        protected void gvInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvInfo.EditIndex = -1;
            BindDetails();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ddlJourn.Visible = false;
            txtJourn.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //entyCom = new entyCommodity();
            //entyCom.BCode = txtByrCd.Text;
            //entyCom.Category = txtAddCategory.Text;
            //entyCom.CName = txtAddCommonNm.Text;
            //entyCom.SName = txtAddSciName.Text;
            //entyCom.Synonym = txtAddSynonym.Text;
            //entyCom.SubSpcs = txtAddSubSpc.Text;
            //entyCom.FName = txtAddFNm.Text;
            //entyCom.OName = txtOdrNm.Text;
            //(new DLayer()).SaveComm(entyCom);
            //BindDetails();
            //clear();
            //pnlAdd.Visible = true;

            entyinfo = new entyInfo();
            entyinfo.InfoCd = txtcd.Text;
            entyinfo.Author = txtAthr.Text+" ";
            entyinfo.Year = txtYr.Text;
            entyinfo.Title = txtTPub.Text;
            entyinfo.Journal = txtJourn.Text;
            entyinfo.Vol = txtVol.Text;
            entyinfo.Pageref = txtPgRef.Text;
            entyinfo.Note = txtNote.Text;
            (new DLayer()).SaveInfo(entyinfo);
            BindDetails();
            clear();
        }

    }
}